﻿using System;
using System.Threading;
using System.Diagnostics;

namespace scomA2Proj
{

    class OrderProcessingThread
    {
        int threadID;
        Order order;
        private decimal taxRate;
        private decimal shippingCostPerChicken;
        private decimal costPerChicken;
        public static event OrderCompleteEvent orderComplete;

        //orderprocessing thread
        //1. validate card num
        //2. calculate total charge of order
        //3. send conf. to retailer
        //4. terminate thread 
        //constructor
        public OrderProcessingThread(Order order, decimal taxRate, decimal shippingCostPerChicken, decimal costPerChicken)
        {
            this.order = order;
            threadID = order.SenderID;
            this.taxRate = taxRate;
            this.shippingCostPerChicken = shippingCostPerChicken;
            this.costPerChicken = costPerChicken;
        }         

        public void runOrderProcessingThread()
        {
            OrderReciept r;
            
            if (validateCC(order.CardNo))
            {
                r = new OrderReciept(order.stopwatch, order.SenderID, order.Amount,
                    costPerChicken, taxRate, shippingCostPerChicken, OrderReciept.OrderResult.Success);
            }
            else
            {
                r = new OrderReciept(order.stopwatch, 0, 0,
                    0, 0, 0, OrderReciept.OrderResult.FailCC);
            }
                //send retailer conf
            sendConf(r);
            
        }

        /// <summary>
        /// Sends confirmation to the retailer that the order was completed, 
        /// allowing the retailer to place an additional order if needed
        /// </summary>
        /// <param name="retailer"></param>
        private void sendConf(OrderReciept orderReceipt)
        {
            if(orderReceipt != null)
                orderComplete(orderReceipt);
        }

        /// <summary>
        /// Checks to see if the retailer has provided a valid credit card
        /// number for the order, must be true for the order to process
        /// </summary>
        /// <param name="ccNum"></param>
        /// <returns></returns>
        private bool validateCC(int ccNum)
        {
            //check CC rules
            return (ccNum >= 10000 && ccNum < 20000);
        }
    }
}